<template>
  <div class="type-nav">
    <div class="container">
      <div @mouseleave="loseShow" @mouseenter="showNav">
        <h2 class="all">全部商品分类</h2>
        <transition name="nav">
        <div class="sort" v-show="show">
        <div class="all-sort-list2" @click="goSearch">
          <div class="item" v-for="(item1,index) in categ" :key="item1.categoryId" @mouseenter="hasShow(index)" :class="{hshow:haveShow==index}" >
            <h3>
              <a :data-categoryName="item1.categoryName" :data-category1Id="item1.categoryId">{{item1.categoryName}}</a>
            </h3>
            <div class="item-list clearfix" :style="{display:haveShow==index?'block':'none'}">
              <div class="subitem" >
                <dl class="fore" v-for="item2 in item1.categoryChild" :key="item2.categoryId" >
                  <dt>
                    <a :data-categoryName="item2.categoryName" :data-category2Id="item2.categoryId">{{item2.categoryName}}</a>
                  </dt>
                  <dd>
                    <em v-for="item3 in item2.categoryChild" :key="item3.categoryId">
                      <a :data-categoryName="item3.categoryName" :data-category3Id="item3.categoryId">{{item3.categoryName}}</a>
                    </em>
                  </dd>
                </dl>
              </div>
            </div>
          </div>
        </div>
      </div>
        </transition>
      </div>
      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
    </div>
  </div>
</template>
<script>
import {mapState} from 'vuex'
import throttle from 'lodash/throttle'
import search from '@/store/search';
export default {
    name:"typeNav",
    data() {
      return {
        haveShow:-1,
        show:true,
      }
    },
    mounted(){
      if (this.$route.path!='/home') {
        this.show=false;
      }
      // this.$store.dispatch("getreqCategoryList")//放在此处 访问次数ajax次数过多，但是导航基本只需访问一次，将其放到其他位置,根组件只会执行一次
     
    },
    methods:{
      // hasShow(index){
      //   console.log(index)
      //   this.haveShow=index
      // },
      //虽说是节流操作，但在此处没感觉出效果，肯定是有用的，还能说因为我时间间隔设置的太短了
      hasShow:throttle(function(index){
        this.haveShow=index
      },40),
      loseShow(){
        this.haveShow=-1;
        if (this.$route.path!='/home') {
          this.show=false;
        }
      },
      goSearch(event){
        let element=event.target; //可以得到标签
        // console.log(element);
        let value1=element.dataset;//可以得到标签的内容，以对象的形式
        // console.log(value1);
        let {categoryname,category1id,category2id,category3id}=value1;
        // console.log(categoryname);
        if(categoryname){
          let location={name:"search"};
          // console.log(location);
          // console.log(categoryname);
          let query={categoryName:categoryname};
          // console.log(query);
          if(category1id){
              query.category1Id=category1id;
            }else if (category2id) {
              query.category2Id=category2id;
            }else if (category3id) {
              query.category3Id=category3id;
            }
            // console.log(this.$route.params)
        location.query=query;
        if (this.$route.params) {
          location.params=this.$route.params;
        }
        // console.log(location);
        this.$router.push(location);
        // this.$router.push({name:"search",query:{}})
        }
      },
      showNav(){
        this.show=true;
      }
    },
    computed:{
       ...mapState({
        //  categ:state.hone.reqCategoryList
         categ:(state)=>{
        // console.log(state) 
        return state.home.reqCategoryList
      },
      })
    },
};
</script>
<style lang="less" scoped>
.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;

      .all-sort-list2 {
        .item {
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #333;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }

          // &:hover {
          //   .item-list {
          //     display: block;
          //   }
          // }
        }
        .hshow{
          background-color: skyblue;  
        }
      }
    }
    .nav-enter,.nav-leave-to{
      height: 0;
    }
    .nav-enter-to,.nav-leave{
      height: 460px;
    }
    .nav-enter-active,.nav-leave-active{
      transition: all 0.3s linear;
    }
  }
}
</style>